/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package reminder;

import configure.ConfigureInfo;
import java.io.*;
import java.net.*;
import java.util.*;
import configure.ConfigureInfo;
import dataaccess.DataSourceFactory;
import dataaccess.IDataSource;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author littlechild
 */
public class ReminderBackActor extends Thread{
    int sleeptime=Integer.parseInt(ConfigureInfo.getProperty(ConfigureInfo.ReminderTimeval));;
    int port=Integer.parseInt(ConfigureInfo.getProperty(ConfigureInfo.ReminderPort));
    Vector<ReminderObject> reminders=null;
    String filename=ConfigureInfo.getProperty(ConfigureInfo.Reminders);
    public static void main(String []args) { 
        new ReminderBackActor().handle();
    }
    private ReminderBackActor() {
    }
    private void init() {
        FileInputStream fin=null;
        try{
            fin=new FileInputStream(filename); 
            reminders=(Vector)
                (new ObjectInputStream(fin).readObject());   
        } catch(FileNotFoundException ex) {
            reminders=new Vector<ReminderObject>();
        }catch (IOException ex) {
            Logger.getLogger(ReminderBackActor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ReminderBackActor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void handle() {
        try {
            init();
            this.start();
            ServerSocket svrsock = new ServerSocket(port);
            while (true) {
                Socket sk = svrsock.accept();
                process(sk);
            }
        } catch (IOException ex) {
            Logger.getLogger(ReminderBackActor.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    public void process(Socket sock) {
        try {
            int command = sock.getInputStream().read();
            if (command == ReminderCommand.getReminders) {
                ObjectOutputStream oout=new ObjectOutputStream(sock.getOutputStream());
                oout.writeObject(reminders.toArray(new ReminderObject[0]));
                oout.close();        
            } else if (command == ReminderCommand.addReminder) {
                ObjectInputStream oin=new ObjectInputStream(sock.getInputStream());
                reminders.add((ReminderObject)oin.readObject());
                ObjectOutputStream oout=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
                oout.writeObject(reminders);
                oin.close();
                oout.close();
            } else if (command == ReminderCommand.deleteReminder) {
                DataInputStream oin=new DataInputStream(sock.getInputStream());
                int index=oin.readInt();
                if(index<reminders.size()) {
                    reminders.remove(index);
                    ObjectOutputStream oout=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
                    oout.writeObject(reminders);
                    oout.close();
                }
                oin.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(ReminderBackActor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ReminderBackActor.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    @Override
    public void run() {
        while(true) {
            try {
                IDataSource data=DataSourceFactory.getDataSource(ConfigureInfo.DataSourceClassName);
                for (int i = 0; i < reminders.size(); ++i) {
                    ReminderObject ro = reminders.get(i);
                    System.out.println("reminder:"+ro);
                    if (ro!=null && ro.isSatisfied(data)) {
                        ro.conduct();
                    }
                }
                System.out.println("here in back:"+System.nanoTime());
                if(ConfigureInfo.isDebug)
                    sleep(600000);
                else
                    sleep(sleeptime);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ReminderBackActor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(ReminderBackActor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(ReminderBackActor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex) {
                Logger.getLogger(ReminderBackActor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
